package cn.imvc.core;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.lang.reflect.Method;

@DisallowConcurrentExecution
public class IJobDetailFactoryBean extends QuartzJobBean{
	private String targetObject;
	private String targetMethod;
	private ApplicationContext applicationContext;

	public String getTargetObject() {
		return targetObject;
	}

	public void setTargetObject(String targetObject) {
		this.targetObject = targetObject;
	}

	public String getTargetMethod() {
		return targetMethod;
	}

	public void setTargetMethod(String targetMethod) {
		this.targetMethod = targetMethod;
	}

	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		try {
			Object otargetObject = getApplicationContext()
					.getBean(targetObject);
			Method method = null;
			try {
				method = otargetObject.getClass().getMethod(targetMethod,
						new Class[] { JobExecutionContext.class });
				method.invoke(otargetObject, new Object[] { context });
			} catch (SecurityException e) {
			} catch (NoSuchMethodException e) {
			}
		} catch (Exception e) {
			throw new JobExecutionException(e);
		}
	}
}